import { expect } from 'chai';
import checkValid from '../src/checkValid.js';

describe('checkValid unit test', function() {
    it('should throw error', function() {
        const runTest = function() { checkValid(); };
        expect(runTest).to.throw('Invalid arguments');
    });

    it('should return valid:false', function() {
        const params = { name: '' };
        const cases = [{ key:'name', required: true, msg: 'name is required' }];
        expect(checkValid(params, cases)).to.be.eql({ valid: false, msg: 'name is required' });
    });

    it('should return valid:true', function() {
        const params = { name: 'foobar' };
        const cases = [{ key:'name', required: true, msg: 'name is required' }];
        expect(checkValid(params, cases)).to.be.eql({ valid: true });
    });

    it('should return valid:false', function() {
        const params = { foobar: 'foobar' };
        const cases = [{ key:'foobar', phone: true, msg: 'foobar is not phone' }];
        expect(checkValid(params, cases)).to.be.eql({ valid: false, msg: 'foobar is not phone' });
    });

    it('should return valid:true', function() {
        const params = { foobar: '12345678901' };
        const cases = [{ key:'foobar', phone: true, msg: 'foobar is not phone' }];
        expect(checkValid(params, cases)).to.be.eql({ valid: true });
    });

    it('should return valid:false', function() {
        const params = { foobar: '' };
        const cases = [
            { key:'foobar', required: true, msg: 'foobar is required' },
            { key:'foobar', phone: true, msg: 'foobar is not phone' },
        ];
        expect(checkValid(params, cases)).to.be.eql({ valid: false, msg: 'foobar is required' });
    });

    it('should return valid:false', function() {
        const params = { foobar: 'abcd' };
        const cases = [
            { key:'foobar', required: true, msg: 'foobar is required' },
            { key:'foobar', phone: true, msg: 'foobar is not phone' },
        ];
        expect(checkValid(params, cases)).to.be.eql({ valid: false, msg: 'foobar is not phone' });
    });
});
